package com.zzjson.type;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.type</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年08月19日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */


import java.util.HashMap;
import java.util.Map;

public class EnumIntegerCodeTypeHandler<E extends Enum> extends AbstractIntegerTypeHandler<E> {

	private Class<E> enumClass;
	private Map<Integer, E> caches;


	@SuppressWarnings("unchecked")
	public EnumIntegerCodeTypeHandler(Class<E> clazz) {
		if (clazz == null) {
			throw new IllegalArgumentException("Type argument cannot be null");
		}
		enumClass = clazz;
		while (enumClass != null && !enumClass.isEnum()) {
			enumClass = (Class<E>) enumClass.getSuperclass();
		}
		if (enumClass == null) {
			throw new IllegalArgumentException(clazz.getSimpleName() + " does not represent an enum type.");
		}
		E[] enums = this.enumClass.getEnumConstants();
		if (enums == null || enums.length == 0) {
			throw new IllegalArgumentException(enumClass.getSimpleName() + " have not enum item.");
		}
		caches = new HashMap<>();
		// Arrays.stream(enums).forEach(item -> caches.put(item.getJdbcValue(), item));
	}

	@Override
	protected int toInteger(E value) {
		// return value.getJdbcValue();
		return 1;
	}

	@Override
	protected E fromInteger(int code) {
		// if (!caches.containsKey(code)) {
		// 	throw new IllegalArgumentException("Cannot convert " + code + " to " + enumClass.getSimpleName() + " by code value.");
		// }
		// return caches.get(code);
		return (E) TestEnum.A;
	}


}